<!--  -->
<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 默认头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <el-form
        label-width="0"
        class="login_form"
        ref="loginForm"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名"
            clearable
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-3702mima"
            clearable
            show-password
            placeholder="请输入密码"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名长度3~10', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度6~15', trigger: 'blur' }
        ]
      }
    }
  },

  created() {},

  methods: {
    // 重置表单
    resetLoginForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 登录
    login() {
      // 登录验证
      this.$refs.loginForm.validate(valid => {
        // 验证失败，结束
        if (!valid) return false
        // 验证成功，发送登录请求
        this.axios
          .post('login', {
            username: this.loginForm.username,
            password: this.loginForm.password
          })
          .then(resp => {
            if (resp.meta.status === 200) {
              // 后台登录验证成功
              this.$message.success('恭喜！登录成功')
              // 1、保存token
              // 1.1、项目中除了登录之外的其他API，只有登录之后才能访问
              // 1.2、token只在当前网站打开其他有效，所有保存到sessionStorage中
              sessionStorage.setItem('token', resp.data.token)
              // 2、通过编程式导航,跳转后台管理首页
              this.$router.push('/home')
            } else {
              this.$message.error('登录失败：用户名/密码有误！')
            }
          })
      })
    }
  },

  components: {}
}
</script>

<style lang='scss' scoped>
// 登录组件
.login_container {
  height: 100%;
  background-color: #2b4b6b;
  // 等了盒子
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    // 头像盒子
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 5px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      // 头像
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    // 表单盒子
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      // 按钮
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
